/*
 * ChooseTaxInvoicePresenter.java
 * business
 *
 * Created by ChenTao on 2017/1/7.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.chooseinvoice.tax;

import android.text.TextUtils;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.TaxInvoice;
import com.yidiandao.business.data.model.IInvoiceModel;
import com.yidiandao.business.data.model.InvoiceModel;
import com.yidiandao.business.utils.RegexUtils;
import com.yidiandao.business.utils.UIUtils;

/**
 * 选择增值税发票-Presenter
 */
public class ChooseTaxInvoicePresenter implements ChooseTaxInvoiceContract.Presenter {

    private ChooseTaxInvoiceContract.View mTaxInvoiceView;

    private IInvoiceModel mInvoiceModel;

    private boolean emptyInvoiceData;

    private TaxInvoice serverTaxInvoice;

    private String mInvType;

    public ChooseTaxInvoicePresenter(String invType, ChooseTaxInvoiceContract.View taxInvoiceView) {
        mTaxInvoiceView = taxInvoiceView;
        mInvType = invType;

        mTaxInvoiceView.setPresenter(this);
        mInvoiceModel = new InvoiceModel();
    }

    @Override
    public void start() {

        mTaxInvoiceView.showLoadingDialog();
        mInvoiceModel.getTaxInvoice(mTaxInvoiceView.getPagerTag(), new IInvoiceModel.OnLoadTaxInvoiceListener() {
            @Override
            public void onSuccess(TaxInvoice taxInvoice) {
                serverTaxInvoice = taxInvoice;
                if (taxInvoice != null) {
                    mTaxInvoiceView.stopInputContent();
                    mTaxInvoiceView.refreshUI(taxInvoice);
                    emptyInvoiceData = false;
                } else {
                    emptyInvoiceData = true;
                }

                mTaxInvoiceView.hideLoadingDialog();
            }

            @Override
            public void onFailed() {
                mTaxInvoiceView.hideLoadingDialog();
            }
        });
    }

    @Override
    public boolean checkInputAddData() {
        if (TextUtils.isEmpty(mTaxInvoiceView.getCompanyName())) {
            mTaxInvoiceView.showToast(UIUtils.getString(R.string.enter_company_name_hint));
            return false;
        }

        if (TextUtils.isEmpty(mTaxInvoiceView.getTaxpayerNumber())) {
            mTaxInvoiceView.showToast(UIUtils.getString(R.string.enter_taxpayer_number_hint));
            return false;
        }

        if (TextUtils.isEmpty(mTaxInvoiceView.getRegisterAddress())) {
            mTaxInvoiceView.showToast(UIUtils.getString(R.string.enter_register_address_hint));
            return false;
        }

        if (TextUtils.isEmpty(mTaxInvoiceView.getBankName())) {
            mTaxInvoiceView.showToast(UIUtils.getString(R.string.enter_deposit_bank_hint));
            return false;
        }

        if (TextUtils.isEmpty(mTaxInvoiceView.getBankNumber())) {
            mTaxInvoiceView.showToast(UIUtils.getString(R.string.enter_deposit_number_hint));
            return false;
        }

        if (TextUtils.isEmpty(mTaxInvoiceView.getReceiverName())) {
            mTaxInvoiceView.showToast(UIUtils.getString(R.string.enter_receiver_name_hint));
            return false;
        }

        if (TextUtils.isEmpty(mTaxInvoiceView.getReceiverNumber())) {
            mTaxInvoiceView.showToast(UIUtils.getString(R.string.enter_receiver_number_hint));
            return false;
        } else {
            if (!RegexUtils.isPhoneNumber(mTaxInvoiceView.getReceiverNumber())) {
                mTaxInvoiceView.showToast(UIUtils.getString(R.string.input_right_number));
                return false;
            }
        }

        if (TextUtils.isEmpty(mTaxInvoiceView.getReceiverAddress())) {
            mTaxInvoiceView.showToast(UIUtils.getString(R.string.enter_receiver_address_hint));
            return false;
        }

        return true;
    }

    @Override
    public void addTaxInvoice() {

        if (!checkInputAddData()) {
            return;
        }

        // 添加增值税发票
        final TaxInvoice taxInvoice = new TaxInvoice();
        taxInvoice.setCompanyName(mTaxInvoiceView.getCompanyName());
        taxInvoice.setTaxCode(mTaxInvoiceView.getTaxpayerNumber());
        taxInvoice.setRegisterAddress(mTaxInvoiceView.getRegisterAddress());
        taxInvoice.setRegisterPhone(mTaxInvoiceView.getRegisterNumber());
        taxInvoice.setBankName(mTaxInvoiceView.getBankName());
        taxInvoice.setBankAccount(mTaxInvoiceView.getBankNumber());
        taxInvoice.setReceiceName(mTaxInvoiceView.getReceiverName());
        taxInvoice.setReceiveMobile(mTaxInvoiceView.getReceiverNumber());
        taxInvoice.setReceiveAddress(mTaxInvoiceView.getReceiverAddress());

        mInvoiceModel.updateTaxInvoice(taxInvoice, mTaxInvoiceView.getPagerTag(), new IInvoiceModel.OnLoadTaxInvoiceListener() {
            @Override
            public void onSuccess(TaxInvoice invoice) {
                mTaxInvoiceView.stopInputContent();
                changeTaxInvoice(taxInvoice);
            }

            @Override
            public void onFailed() {
            }
        });
    }

    @Override
    public void changeTaxInvoice(TaxInvoice taxInvoice) {

        mInvoiceModel.changeInvoice(mTaxInvoiceView.getPagerTag(), mInvType, taxInvoice.getCompanyName(), new IInvoiceModel.OnNetAccessListener() {
            @Override
            public void onSuccess() {
                mTaxInvoiceView.finishActivity();
            }

            @Override
            public void onFailed() {

            }
        });

    }

    @Override
    public void onConfirmTaxInvoice() {
        if (emptyInvoiceData) {
            // 需要先添加默认的增值税发票
            addTaxInvoice();
        } else {
            // 直接保存增值税发票
            changeTaxInvoice(serverTaxInvoice);
        }
    }
}
